package com.weight.system.overload.web;

import com.weight.system.overload.PO.OverloadInfo;
import com.weight.system.overload.VO.OverloadInfoVO;
import com.weight.system.overload.service.OverloadInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 超重记录表
 */
@RestController
@Slf4j
@RequestMapping("/overload")
public class OverloadInfoController {

	@Autowired
	private OverloadInfoService overloadInfoService;

	private String pattern = "yyyy-MM-dd";

	 
	/**
	 * 1.查询超重记录表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForOverloadInfoList",method=RequestMethod.GET)
	public ModelAndView queryForOverloadInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("overload/queryForOverloadInfoList");
		 
			Map<Object, Object> map =new HashMap<>();
			map.put("standbyfield1", "1");
			  
			//2、查询超重记录表列表
			List<OverloadInfo> overloadInfoList = overloadInfoService.findOverloadInfoByT(map);
			log.info("overloadInfoList={}",overloadInfoList);
			List<OverloadInfoVO> overloadInfoVOList = overloadInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			
			modelAndView.addObject("overloadInfoVOList",overloadInfoVOList);
			return modelAndView;
	}

	/**
	 * 2.查询超重记录表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForOverloadInfoListDG",method=RequestMethod.GET)
	public String queryForOverloadInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String equmtId = request.getParameter("equmtId");
			String orgId = request.getParameter("orgId");
			String overloadCount = request.getParameter("overloadCount");
			String overloadDate = request.getParameter("overloadDate");

			Map<Object, Object> map =new HashMap<>();
			map.put("equmtId", equmtId);
			map.put("orgId", orgId);
			map.put("overloadCount", overloadCount);
			map.put("overloadDate", DateFormUtils.getStringToDate(overloadDate, pattern));

			
			//2、查询超重记录表列表
			List<OverloadInfo> overloadInfoList = overloadInfoService.findOverloadInfoByPage(page,rows,map);
			log.info("overloadInfoList={}",overloadInfoList);
			List<OverloadInfoVO> overloadInfoVOList = overloadInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<OverloadInfoVO> result = new PageResult<OverloadInfoVO>(overloadInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param overloadInfoVO
	 * @param overloadInfo
	 * @return
	 */
	public void changeVOToPO(OverloadInfoVO overloadInfoVO,OverloadInfo overloadInfo){
			BeanUtils.copyProperties(overloadInfoVO,overloadInfo);
			overloadInfo.setOverloadDate(DateFormUtils.getStringToDate(overloadInfoVO.getOverloadDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param overloadInfoVO
	 * @param overloadInfo
	 * @return
	 */
	public void changePoToVO(OverloadInfoVO overloadInfoVO,OverloadInfo overloadInfo){
			BeanUtils.copyProperties(overloadInfo,overloadInfoVO);
			overloadInfoVO.setOverloadDate(DateFormUtils.getDateToString(overloadInfo.getOverloadDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param overloadInfo
	 * @return
	 */
	public OverloadInfoVO changePoToVO(OverloadInfo overloadInfo){
			OverloadInfoVO overloadInfoVO=new OverloadInfoVO();
			BeanUtils.copyProperties(overloadInfo,overloadInfoVO);
			overloadInfoVO.setOverloadDate(DateFormUtils.getDateToString(overloadInfo.getOverloadDate(), pattern));
			

			return overloadInfoVO;
	}

	/**
	 * 3.查询超重记录表--详情页
	 * @param overloadId
	 * @return
	 */
	@RequestMapping("/queryForOverloadInfoDetail")
	public ModelAndView queryForOverloadInfoDetail(@RequestParam(value="overloadId",required=false) String overloadId){
			//1.校验id
			CommonUtils.validateExistID(overloadId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("overloadId", overloadId);
			log.info("查询超重记录表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			OverloadInfo overloadInfo = overloadInfoService.findByOverloadInfoId(map);
			
			OverloadInfoVO overloadInfoVO = changePoToVO(overloadInfo);
			//3、返回前台
			log.info("查询超重记录表查询成功：overloadInfo={}",overloadInfo);
			modelAndView.setViewName("/system/overload/queryForOverloadInfoDetail");
			modelAndView.addObject("overloadInfoVO",overloadInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改超重记录表操作
	 * @param overloadId
	 * @return
	 */
	@RequestMapping(value="/addOverloadInfo")
	public ModelAndView addOverloadInfo(@RequestParam(value="overloadId",required=false) String overloadId){
			//1、创建返回对象
			OverloadInfoVO overloadInfoVO = new OverloadInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(overloadId)) {
				Map<String, String> map=new HashMap<>();
				map.put("overloadId", overloadId);
				log.info("修改操作，查询ID：overloadId={}",overloadId);
				OverloadInfo overloadInfo = overloadInfoService.findByOverloadInfoId(map);
				this.changePoToVO(overloadInfoVO,overloadInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/overload/addOverloadInfo");
			modelAndView.addObject("overloadInfoVO",overloadInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改超重记录表操作
	 * @param overloadInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveOverloadInfo")
	public JsonResultUtil saveOverloadInfo(@Valid OverloadInfoVO overloadInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,overloadInfoVO);
			
			//2.获取id
			String overloadId=overloadInfoVO.getOverloadId();
			OverloadInfo overloadInfo=new OverloadInfo();
			BeanUtils.copyProperties(overloadInfoVO,overloadInfo);
			this.changeVOToPO(overloadInfoVO,overloadInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(overloadId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("overloadId", overloadId);
				log.info("修改操作，查询ID：overloadId={}",overloadId);
				//查询是否存在
				overloadInfoService.findByOverloadInfoId(map);
				try{
					overloadInfo.setUpdateBy("000");
					overloadInfoService.updateOverloadInfo(overloadInfo);
					log.info("更新操作成功：overloadInfo={}",overloadInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					overloadInfo.setOverloadId(CommonUtils.getUUId());
					overloadInfo.setCreateBy("000");
					overloadInfo.setUpdateBy("000");
					overloadInfoService.insertOverloadInfo(overloadInfo);
					log.info("添加操作成功：overloadInfo={}",overloadInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除超重记录表操作
	 * @param overloadId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByOverloadInfoId")
	public JsonResultUtil deleteByOverloadInfoId(@RequestParam(value="overloadId",required=false) String overloadId){
			//1.校验id
			CommonUtils.validateExistID(overloadId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("overloadId", overloadId);
			overloadInfoService.findByOverloadInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除超重记录表入参，map={}",map);
			
			try{
				overloadInfoService.upDelByOverloadInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除超重记录表操作---批量删除
	 * @param overloadIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByOverloadInfoIdBatch")
	public JsonResultUtil deleteByOverloadInfoIdBatch(@RequestParam(value="overloadIdList",required=false) List overloadIdList){
			//1.校验id
			CommonUtils.validateExistIDList(overloadIdList);
			
			//2.校验数据库
			overloadInfoService.findByOverloadInfoIdList(overloadIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("overloadIdList", overloadIdList);
			map.put("updateBy","000");
			log.info("批量删除超重记录表操作入参，map={}",map);
			
			try{
				overloadInfoService.upDelByOverloadInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}